// 导入用户API
import { userAPI } from './TabAxios/user'

// 创建简单的数据API和系统API（如果不存在的话）
const dataAPI = {
    getList: async(params = {}) => {
        console.log('📊 [DataAPI] 获取数据列表:', params)
        // 这里可以根据实际需要实现具体的数据获取逻辑
        return { data: [], total: 0 }
    },
    save: async(params = {}) => {
        console.log('💾 [DataAPI] 保存数据:', params)
        // 这里可以根据实际需要实现具体的数据保存逻辑
        return { success: true }
    }
}

const systemAPI = {
    getInfo: async(params = {}) => {
        console.log('⚙️ [SystemAPI] 获取系统信息:', params)
        // 这里可以根据实际需要实现具体的系统信息获取逻辑
        return { version: '1.0.0', status: 'running' }
    }
}

const cefsharpAdapter = {
    checkEnvironment: () => typeof window.cefSharpServer !== 'undefined',
    callDbMethod: (methodName, params = {}) =>
        new Promise((resolve, reject) => {
            if (typeof window.cefSharpServer === 'undefined') {
                reject(new Error('cefSharp环境不可用'))
                return
            }
            
            try {
                // 获取全局的分组设备id
                let groupDeviceId = 0
                if (window.$store) {
                    const storeGroupDeviceId = window.$store.getters['global/getCurrentGroupDeviceId']
                    if (storeGroupDeviceId !== null && storeGroupDeviceId !== undefined) {
                        groupDeviceId = storeGroupDeviceId
                    }
                }
                
                window.cefSharpServer
                    .callDbMethod(methodName, params, groupDeviceId)
                    .then(resolve)
                    .catch(reject)
            } catch (error) {
                reject(error)
            }
        }),

    callNativeMethod: (methodName, params = {}) =>
        new Promise((resolve, reject) => {
            if (typeof window.cefSharpNative === 'undefined') {
                reject(new Error('cefSharp环境不可用'))
                return
            }
            
            try {
                // 获取全局的分组设备id
                let groupDeviceId = 0
                if (window.$store) {
                    const storeGroupDeviceId = window.$store.getters['global/getCurrentGroupDeviceId']
                    if (storeGroupDeviceId !== null && storeGroupDeviceId !== undefined) {
                        groupDeviceId = storeGroupDeviceId
                    }
                }
                
                console.log('🔍 [CefSharp] 分组设备id:', { 
                    methodName, 
                    groupDeviceId,
                    hasStore: !!window.$store,
                    storeValue: window.$store ? window.$store.getters['global/getCurrentGroupDeviceId'] : 'no store'
                })
                
                window.cefSharpNative
                    .callNativeMethod(methodName, params, groupDeviceId)
                    .then(resolve)
                    .catch(reject)
            } catch (error) {
                reject(error)
            }
        })        
}

// 导出所有API
export { userAPI, dataAPI, systemAPI }
export default cefsharpAdapter
